package middleware

import (
	o "gostore/common"
	"gostore/util"
	"gostore/util/log"

	"github.com/gin-gonic/gin"
)
// @Summary 订单信息
// @Description  通过id获取对应订单信息
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Success 200 {string} string "{"code"":0,"data":"","msg":"ok"}"
// @Router /api/captcha [get]
func GetCaptcha(c *gin.Context) {
	id, b64s, err := util.GenerateCaptchaRand()
	if err != nil {
		o.Err(c, 1, err.Error())
		return
	}
	o.Ok(c, map[string]string{
		"id": id, "pic": b64s,
	})
}

// @Summary 订单信息
// @Description  通过id获取对应订单信息
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Success 200 {string} string "{"code"":0,"data":"","msg":"ok"}"
// @Router /api/captcha/verify [post]
func CaptchaVerify(c *gin.Context) {
	obj := struct {
		Id    string
		Value string
	}{}
	err := c.BindJSON(&obj)
	// id := c.Request.PostFormValue("id")
	// value := c.Request.PostFormValue("value")
	log.Debug(obj)
	// log.Debug(id, value)
	if err != nil {
		o.Err(c, 1, err.Error())
		return
	}
	ok, _ := util.CaptchaVerify(obj.Id, obj.Value)
	if ok {
		o.Ok(c, "ok")
	} else {
		o.Err(c, 1, "验证码错误")
	}
}
